﻿using Flurl.Http;
using HandyControl.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using wms_wpf.Dto;
using wms_wpf.InPut;

namespace wms_wpf.Apiservice
{
    public class ViewService : IViewService
    {
        string config = Environment.GetEnvironmentVariable("上海铁路通信-config", EnvironmentVariableTarget.Machine);

        public async Task<Tuple<List<View_BoxDto>, long?, long?>> GetList_View_Box(InPut_ViewService5 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_Box";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_BoxDto> res = JsonConvert.DeserializeObject<List<View_BoxDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_Box", ex);
                return null;
            }
        }
        public async Task<Tuple<List<View_InlethouseLineDto>, long?, long?>> GetList_View_InlethouseLine(InPut_ViewService3 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_InlethouseLine";

                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_InlethouseLineDto> res = JsonConvert.DeserializeObject<List<View_InlethouseLineDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_InlethouseLine", ex);
                return null;
            }
        }
        public async Task<Tuple<List<View_MaterialsDto>, long?, long?>> GetList_View_Materials(InPut_ViewService Input)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_Materials";
                var data = Input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_MaterialsDto> res = JsonConvert.DeserializeObject<List<View_MaterialsDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }


            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_Materials", ex);
                return null;
            }
        }
        public async Task<Tuple<List<View_NeetOutMaterialDto>, long?, long?>> GetList_View_NeetOutMaterial(InPut_ViewService2 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_NeetOutMaterial";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_NeetOutMaterialDto> res = JsonConvert.DeserializeObject<List<View_NeetOutMaterialDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("GetList_View_NeetOutMaterial接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_NeetOutMaterial", ex);
                return null;
            }
        }
        public async Task<Tuple<List<View_TaskOrderDto>, long?, long?>> GetList_View_TaskOrder(InPut_ViewService4 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_TaskOrder";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_TaskOrderDto> res = JsonConvert.DeserializeObject<List<View_TaskOrderDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_TaskOrder", ex);
                return null;
            }
        }
        public async Task<List<View_UserDto>> GetList_View_User()
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_User";
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostAsync().ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_UserDto> res = JsonConvert.DeserializeObject<List<View_UserDto>>(Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }


            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_User", ex);
                return null;
            }
        }
        public async Task<List<View_Box_MaterialDto>> GetList_View_Box_Material(string BoxCode)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_Box_Material?BoxCode={BoxCode}";
                string result = await url.PostAsync().ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_Box_MaterialDto> res = JsonConvert.DeserializeObject<List<View_Box_MaterialDto>>(Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }


            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_Box_Material", ex);
                return null;
            }
        }
        public async  Task<Tuple<List<View_LogDto>, long?, long?>> GetList_View_Logs(InPut_ViewService6 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_Logs";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_LogDto> res = JsonConvert.DeserializeObject<List<View_LogDto>>(Response.Dto.ToString());
                    return Tuple.Create(res, Response.Count, Response.PageCount);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_Logs", ex);
                return null;
            }
        }
        public async Task<List<View_TaskOrder_OrderNoDto>> GetList_View_TaskOrder_OrderNo()
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_TaskOrder_OrderNo";
           
                string result = await url.PostAsync().ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_TaskOrder_OrderNoDto> res = JsonConvert.DeserializeObject<List<View_TaskOrder_OrderNoDto>>(Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_Logs", ex);
                return null;
            }
        }
        public async Task<List<View_TaskOrderDto>> QuaneryList_View_TaskOrder(InPut_ViewService7 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/QuaneryList_View_TaskOrder";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                    List<View_TaskOrderDto> res = JsonConvert.DeserializeObject<List<View_TaskOrderDto>>(Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("QuaneryList_View_TaskOrder", ex);
                return null;
            }
        }
        public async Task<View_TaskOrderDto> SingleView_TaskOrder(string TaskOrderCode)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/SingleView_TaskOrder?TaskOrderCode={TaskOrderCode}";
                string result = await url.PostAsync().ReceiveString();
                ApiResponsePage Response = JsonConvert.DeserializeObject<ApiResponsePage>(result);
                if (Response.State)
                {
                   View_TaskOrderDto res = JsonConvert.DeserializeObject<View_TaskOrderDto>(Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("SingleView_TaskOrder", ex);
                return null;
            }
        }
        public async Task<List<View_View_repeatOrderDto>> GetList_View_repestorder()
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_repestorder";
                string result = await url.PostAsync().ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    List<View_View_repeatOrderDto> res = JsonConvert.DeserializeObject< List < View_View_repeatOrderDto >> (Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_repestorder:", ex);
                return null;
            }
        }
        public async Task<List<View_ProportionDto>> GetList_View_Proportion(InPut_ViewService8 InPut)
        {
            try
            {
                var url = $"http://{config}/api/ViewService/GetList_View_Proportion";
                string result = await url.PostJsonAsync(InPut).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    List<View_ProportionDto> res = JsonConvert.DeserializeObject<List< View_ProportionDto>>(Response.Dto.ToString());
                    return res;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("GetList_View_Proportion", ex);
                return null;
            }
        }


    }
}
